<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 2023/6/17
  Time: 11:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>客户信息添加页面</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.css">
    <script src="https://cdn.staticfile.org/layui/2.5.6/layui.js"></script>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

</head>
<body>

<div class="layui-container">
    <form class="layui-form" action="#"id="addfm">
        <div class="layui-form-item">
            <label class="layui-form-label">客户id</label>
            <div class="layui-input-block">
                <input type="text" name="clientId" lay-verify="required" placeholder="客户id" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司名称</label>
            <div class="layui-input-block">
                <input type="text" name="clientName" lay-verify="required" placeholder="公司名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司电话</label>
            <div class="layui-input-block">
                <input type="text" name="clientPhone" lay-verify="required" placeholder="公司电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">纳税识别号</label>
            <div class="layui-input-block">
                <input type="text" name="taxpayerNumber" lay-verify="required" placeholder="纳税识别号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司地址</label>
            <div class="layui-input-block">
                <input type="text" name="clientAddress" lay-verify="required" placeholder="公司地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司开户行</label>
            <div class="layui-input-block">
                <input type="text" name="clientBank" lay-verify="required" placeholder="公司开户行" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司银行号</label>
            <div class="layui-input-block">
                <input type="text" name="clientBankNumber" lay-verify="required" placeholder="公司银行号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人名称</label>
            <div class="layui-input-block">
                <input type="text" name="contactName" lay-verify="required" placeholder="联系人名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人电话</label>
            <div class="layui-input-block">
                <input type="text" name="contactPhone" lay-verify="required" placeholder="联系人电话" autocomplete="off" class="layui-input">
            </div>-
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户类型</label>
            <div class="layui-input-block">
                <select name="clientType" lay-verify="required" id="clientType">
                    <option value="-1">请选择客户类型</option>
                    <option value="1">供应商</option>
                    <option value="2">企业客户</option>
                    <option value="3">批发商</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户公司抬头</label>
            <div class="layui-input-block">
                <input type="text" name="clientTitle" lay-verify="required" placeholder="客户公司抬头" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="clientSrc" placeholder="请输入备注描述" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit onclick="addClienta()" lay-filter="submitForm">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>




<script>
    layui.use('form', function(){
        var form = layui.form;

        // 监听表单提交
        form.on('submit(submitForm)', function(data){
            // 提交表单的逻辑处理
            console.log(data.field);
            return false; // 阻止表单跳转
        });
    });
</script>

<script type="text/javascript">

        function addClienta() {
        var  Client= fmobjToObj($("#addfm"));
        $.ajax({
            url     :  '${pageContext.request.contextPath}/ClientS/addCli',    // 要提交的URL路径
            type    :  "post",      // 发送请求的方式
            contentType:"application/json",
            data    : JSON.stringify(Client) ,      // 要发送到服务器的数据
            dataType :  "json",     // 预期接收到的数据格式
            "beforeSend": function() {     //请求之前的方法
                console.log("我要发送请求了");
            },
            "success"  :  function(result) {  // 请求成功后要执行的代码
                alert(JSON.stringify(result))
                if (result.code===20000){
                    alert("添加成功");
                    window.location.href="toclientlist";
                }else {
                    alert("添加失败");
                }
            },
            "error"       :  function() {     // 请求失败后要执行的代码
                alert("网络正忙，请稍等！！！")
            }
        })
    }

    function  fmobjToObj(fmobj){
        var o = {};
        var a = fmobj.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }
</script>
</body>
</html>
